package com.sunny.ynhz.ui;

import android.app.Activity;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.sunny.ynhz.R;
import com.sunny.ynhz.api.okhttp.OkHttpHelper;
import com.sunny.ynhz.utils.FileUtils;
import com.yqritc.scalablevideoview.ScalableVideoView;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * 播放视频页面
 *
 * @author Martin
 */
public class PlayVideoActiviy extends Activity {

    public static final String TAG = "PlayVideoActiviy";

    public static final String KEY_FILE_PATH = "file_path";

    private String filePath;

    private ScalableVideoView mScalableVideoView;
    private ImageView mPlayImageView;
    private ImageView mThumbnailImageView;
    private PlayVideoActiviy activity;
    private File file;
    private boolean ddd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = this;

        filePath = getIntent().getStringExtra(KEY_FILE_PATH);
        Log.d(TAG, filePath);
        if (TextUtils.isEmpty(filePath)) {
            Toast.makeText(this, "视频路径错误", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        setContentView(R.layout.activity_play_video);
        mScalableVideoView = (ScalableVideoView) findViewById(R.id.video_view);
        try {
            // 这个调用是为了初始化mediaplayer并让它能及时和surface绑定
            mScalableVideoView.setDataSource("");
        } catch (IOException e) {
            e.printStackTrace();
        }

        mPlayImageView = (ImageView) findViewById(R.id.playImageView);

        mThumbnailImageView = (ImageView) findViewById(R.id.thumbnailImageView);
        mThumbnailImageView.setImageBitmap(getVideoThumbnail(filePath));
        if (getIntent().getIntExtra("dataSrc", 0) == 0) {
            getVedioFromInternet();
        }

    }

    /**
     * 获取视频缩略图（这里获取第一帧）
     *
     * @param filePath
     * @return
     */
    public Bitmap getVideoThumbnail(String filePath) {
        Bitmap bitmap = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            retriever.setDataSource(filePath);
            bitmap = retriever.getFrameAtTime(TimeUnit.MILLISECONDS.toMicros(1));
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } finally {
            try {
                retriever.release();
            } catch (RuntimeException e) {
                e.printStackTrace();
            }
        }
        return bitmap;
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.video_view:
                mScalableVideoView.stop();
                mPlayImageView.setVisibility(View.VISIBLE);
                mThumbnailImageView.setVisibility(View.VISIBLE);
                break;
            case R.id.playImageView:
                if (getIntent().getIntExtra("dataSrc", 0) == 0) {
                    if (ddd == true) {
                        //split by ' change to Array
                        String[] testArray = filePath.split("/");
                        //print last String
                        setUpVideo(file.getPath() + testArray[testArray.length - 1]);
                    } else {
                        Toast.makeText(this, "视频正在下载,请稍后", Toast.LENGTH_LONG).show();
                    }
                } else setUpVideo(filePath);
                break;
        }
    }

    private void setUpVideo(String filePath) {
        try {

            mScalableVideoView.setDataSource(filePath);
            mScalableVideoView.setLooping(true);
            mScalableVideoView.prepare();
            mScalableVideoView.start();
            mPlayImageView.setVisibility(View.GONE);
            mThumbnailImageView.setVisibility(View.GONE);
        } catch (IOException e) {
            Log.e(TAG, e.getLocalizedMessage());
            Toast.makeText(this, "播放视频异常", Toast.LENGTH_SHORT).show();
        }
    }


    private void getVedioFromInternet() {
        file = FileUtils.createTmpVideo(this, "RiverChief");
        Log.e("TAG", file.getAbsolutePath() + "   " + file.getPath());
        OkHttpHelper.getDownloadDelegate().downloadAsyn(filePath, file.getPath(), (new OkHttpHelper.ResultCallback<String>() {
            @Override
            public void onResponse(String response) {

            }

            @Override
            public void onAfter() {
                super.onAfter();
            }

            @Override
            public void onProgressUpdate(long bytesRead, long contentLength, boolean done) {
                super.onProgressUpdate(bytesRead, contentLength, done);
                ddd = done;

            }

        }), this);
    }

}
